﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using yiCommerce.Controllers;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.Statuses;
using yiCommerce.Core.Domain.Tags;
using yiCommerce.Models.Comments;
using yiCommerce.Models.Products;
using yiCommerce.Models.Statuses;
using yiCommerce.Models.Tags;
using yiCommerce.Service.Pictures;
using Tag = yiCommerce.Core.Domain.Tags.Tag;

namespace yiCommerce.Mapper
{
    public class StatusMapper
    {

        public static StatusModel ToStatusModel(Status status)
        {
            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            var m = new StatusModel()
                {
                    Point = status.Point,
                    Id = status.Id,
                    IsProduct = status.IsProductStatus,
                    Pics = ToStatusPictureModel(status.StatusPictures),
                    Text = status.Text,
                    ProductStatusInfo = ToProductStatusInfo(status.Product),
                    CommentModels = ToCommentModel(status.Comments),
                    PosterLogoUrl = status.Customer.CustomerLogoId.HasValue

                            ? _pictureService.GetPictureUrl(status.Customer.CustomerLogo.PictureId, GetCustomerLogoSize(), false)
                            : _pictureService.GetDefaultPictureUrl(GetCustomerLogoSize())
                };
            return m;
        }

        public static IList<CommentModel> ToCommentModel(ICollection<Comment> comments)
        {
            
            if (comments == null)
            {
                return null;
            }
            else
            {
                return comments.Select(c => ToCommentModel(c)).ToList();
            }
        }

        public static CommentModel ToCommentModel(Comment comment)
        {
              var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            var _workContext =  EngineContext.Current.Resolve<IWorkContext>();
            return new CommentModel()
                {
                    IsMyComment = comment.Commenter_Id == comment.Status.CustomerId, 
                    Text = comment.Text,
                    StatusId = comment.StatusId,
                    CommenterId = comment.Commenter.Id,
                    CreatedAt = comment.CreatedAt,
                    CustomerLogUrl =
                        comment.Commenter.CustomerLogoId.HasValue

                            ? _pictureService.GetPictureUrl(comment.Commenter.CustomerLogo.PictureId, GetCustomerLogoSize(), false)
                            : _pictureService.GetDefaultPictureUrl(GetCustomerLogoSize()),
                };
        }

        public static IList<StatusListItemModel> PrepareStatusListItemModel(IEnumerable<Status> statuses)
        {

            if (statuses == null)
            {
                return new List<StatusListItemModel>();
            }
            else
            {
                return statuses.Select(c =>ToStatusListItemModel(c)).ToList();
            }
        }

        public static IPagedList<StatusListItemModel> ToPagedStatusListItemModels(IPagedList<Status> statuses)
        {
            if (statuses == null)
            {
                return null;
            }
            else
            {
                var tempList  = statuses.Select(c => ToStatusListItemModel(c)).ToList();

                var result = new PagedList<StatusListItemModel>(tempList, statuses.PageIndex, statuses.PageSize, statuses.TotalCount);
                return result;
            }
        }

        private static int GetCustomerLogoSize()
        {
            return int.Parse(ConfigurationManager.AppSettings["CustomerLogoSize"]);
        }
        public static StatusListItemModel ToStatusListItemModel(Status status )
        {

            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            return new StatusListItemModel()
                {
                    Id = status.Id,
                    CreatedAt = status.CreatedAt,
                    Text = KeepLinkOutPut.ToHtmlString(status.Text),
                    CustomerId = status.CustomerId,
                    Deleted = status.Deleted,
                    CustomerLogoUrl = status.Customer.CustomerLogoId.HasValue ? _pictureService.GetPictureUrl(status.Customer.CustomerLogo.PictureId, GetCustomerLogoSize(), false) : _pictureService.GetDefaultPictureUrl(GetCustomerLogoSize()),
                    CustomerEmail = status.Customer.Email,
                    CustomerName = status.Customer.Username,
                    StatusPictures = ToStatusPictureModel(status.StatusPictures),
                    IsProductStatus = status.IsProductStatus,
                    ProductStatus = ToProductStatus(status.Product),
                    StatusTags = ToStatusTags(status.StatusTags),
                };
        }

        private static IList<StatusTagModel> ToStatusTags(ICollection<StatusTag> statusTags)
        {
            if (statusTags == null)
                return null;
            else
            {
                return statusTags.Select(c => new StatusTagModel()
                    {
                        TagName = c.Tag.TagName,
                        TagValue = c.Tag.TagValue
                    }).ToList();
            }
        }

        private static ProductStatus ToProductStatus(Product product)
        {
            if (product == null)
            {
                return null;
            }
            else
            {
                return new ProductStatus()
                    {
                        ProductId = product.Id,
                        ProductPrice = product.ProductVariants.First().Price,
                        ProductStock = product.ProductVariants.First().StockQuantity,
                    };
            }
        }

        private static ProductStatusInfo ToProductStatusInfo(Product product)
        {
            if (product == null)
            {
                return null;
            }
            else
            {
                return new ProductStatusInfo()
                {
                    ProductId = product.Id,
                    PvInfos = ToProductVariantInfo(product)
                };
            }
        }

        private static IList<ProductVariantInfo> ToProductVariantInfo(Product product)
        {
            var result = product.ProductVariants.Select(c => new ProductVariantInfo()
                {
                    ProductStock = c.StockQuantity,
                    ProductPrice = c.Price,
                    ProductVariantId = c.Id
                }).ToList();
            return result;
        }


        private static IList<StatusPictureModel> ToStatusPictureModel(ICollection<StatusPicture> statusPictures)
        {
            var _pictureService = EngineContext.Current.Resolve<IPictureService>();

            if (statusPictures == null)
            {
                return null;
            }
            else
            {
                return statusPictures.Select(c => new StatusPictureModel()
                    {
                        PicId = c.PictureId,
                        PicUrl = _pictureService.GetPictureUrl(c.PictureId, 80, false),

                    }).ToList();
            }
        }

        public static IPagedList<TagListItemModel> ToPagedTagList(IPagedList<Tag> tags)
        {
            if (tags == null)
            {
                return null;
            }
            else
            {
                var tempList = tags.Select(c => new TagListItemModel()
                    {
                        TagId = c.Id,
                        TagName = c.TagName,
                        TagValue = c.TagValue,
                        CountTaged = c.CountTaged,

                        TagIntroStatuses = c.TagIntroStatuses == null ? new List<TagIntroStatusModel>() : ToTagIntroStatusModels(c.TagIntroStatuses)
                    }).ToList();

                var result = new PagedList<TagListItemModel>(tempList, tags.PageIndex, tags.PageSize, tags.TotalCount);
                return result;
            }
        }


        public static IList<TagIntroStatusModel> ToTagIntroStatusModels(ICollection<TagIntroStatuses> statuseses )
        {
            if (statuseses == null)
            {
                return null;
            }
            else
            {
                var result = statuseses.Select(c => new TagIntroStatusModel()
                    {
                        CreatedAt = c.CreatedAt,
                        CustomerId = c.CustomerId,
                        IsProductStatus = c.IsProductStatus,
                        Point = c.Point,
                        ProductId = c.ProductId,
                        Text = c.Text
                    }).ToList();
                return result;
            }
          
        }

        //public static IPagedList<TagIntroListItemModel> ToPagedTagStatusesList(IPagedList<TagIntroStatuses> tagIntros)
        //{
        //    if (tagIntros == null)
        //    {
        //        return null;
        //    }
        //    else
        //    {
        //        var tempList = tagIntros.Select(c => new TagIntroListItemModel()
        //        {
        //            TagId = c..Id,
        //            TagName = c.Tag.TagName,
        //            TagValue = c.Tag.TagValue,
        //            CountTaged = c.Tag.CountTaged,

        //        }).ToList();

        //        var result = new PagedList<TagIntroListItemModel>(tempList, tagIntros.PageIndex, tagIntros.PageSize, tagIntros.TotalCount);
        //        return result;
        //    }
        //}
    }
}